<?php

class DetailController extends Controller
{
	public function actionIndex()
	{
      $this->layout = 'shop';
      $id = Yii::app()->request->getParam('id',0);
      if($id > 0){
         $book = new Books();
         $fields = array('book_id','book_name','book_image','book_price','book_num_page','book_content');
         $book_info = $book->getInfo($id,$fields);
         $publisher = $book->getPublishers($id);
         $category = $book->getCategory($id);
         $authors = $book->getAuthor($id);
         $reviews = $book->getReview($id);
      }
		$this->render('index',array('book_info' => $book_info,
                                  'publisher' => $publisher,
                                  'category' => $category,
                                  'authors' => $authors,
                                  'reviews' => $reviews,
                                  'id' => $id));
	}
   public function actionReview(){
      $book_id = Yii::app()->request->getParam('book_id',0);
      $rate = Yii::app()->request->getParam('rate',0);
      $rev_content = Yii::app()->request->getParam('rev_content',0);
      $date = time();
      $rev_use_id = Yii::app()->user->id;
      if(!is_null($rev_use_id)){         
         $rate = ($rate >= 1 && $rate <= 5) ? $rate : 5;
         $sql = "INSERT INTO reviews(rev_product_id,
                                     rev_use_id,
                                     rev_rating,
                                     rev_date,
                                     rev_content)
                                 VALUES(:product_id,
                                        :use_id,
                                        :rating,
                                        :date,
                                        :content)";
         $db = Yii::app()->db;
         $cmd = $db->createCommand($sql);
         $cmd->bindParam(":product_id",$book_id);
         $cmd->bindParam(":use_id",$rev_use_id);
         $cmd->bindParam(":rating",$rate);
         $cmd->bindParam(":date",$date);
         $cmd->bindParam(":content",$rev_content);
         $add = $cmd->execute();
         if($add){
            echo json_encode(array('error' => 0));
         }else{
            echo json_encode(array('error' => 1));
         }
      }
      
   }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}